using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Core.ServiceImpl.Repositories.Interface;

namespace EzDesk.Core.ServiceImpl.Repositories
{
    public partial class AppFunctionTypeRepository : IAppFunctionTypeRepository
    {
        EzDeskDataContext _db;

        public AppFunctionTypeRepository(EzDeskDataContext db)
        {
            this._db = db;
        }
		
		#region IRepository<AppFunctionType> Members

        public Guid Add(AppFunctionType model, bool saveChanges)
        {   
            _db.ez_AppFunctionTypes.InsertOnSubmit(
                new ez_AppFunctionType()
                {
					AppFunctionTypeId = model.Id,
					
					TypeName = model.TypeName,						
					DisplayText = model.DisplayText,
                    IconName = model.IconName,						
					Privilege = model.Privilege,						
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public AppFunctionType GetById(Guid id)
        {
            FunctionType info = null;
            if (id != Guid.Empty)
            {
                info = _db.ez_AppFunctionTypes.Where(p => p.AppFunctionTypeId == id)
                    .Select(p => new FunctionType()
                    {
						Id = p.AppFunctionTypeId,
						
						TypeName = p.TypeName,						
						DisplayText = p.DisplayText,
                        IconName = p.IconName,						
						Privilege = p.Privilege,						
                    })
                    .SingleOrDefault();
            }
            return info;
        }

        public IQueryable<AppFunctionType> GetAll()
        {
            return _db.ez_AppFunctionTypes.Select(
                p => new FunctionType()
                {
					Id = p.AppFunctionTypeId,
					
					TypeName = p.TypeName,						
					DisplayText = p.DisplayText,
                    IconName = p.IconName,						
					Privilege = p.Privilege,						
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_AppFunctionType entity = _db.ez_AppFunctionTypes.Where(p => p.AppFunctionTypeId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_AppFunctionTypes.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(AppFunctionType model, bool saveChanges)
        {
            var entity = _db.ez_AppFunctionTypes.Where(p => p.AppFunctionTypeId == model.Id)
                .SingleOrDefault();

			entity.TypeName = model.TypeName;
			entity.DisplayText = model.DisplayText;
            entity.IconName = model.IconName;
			entity.Privilege = model.Privilege;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
		#endregion
    }
}
